package cn.dota2info.cglib;

import org.reactivestreams.Subscription;
import reactor.core.publisher.BaseSubscriber;
import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.List;

public class WebFLuxTest {
    public static void main(String[] args) {
//        Flux<Integer> ints = Flux.range(1, 4)
//                .map(i -> {
//                    if (i <= 3) return i;
//                    throw new RuntimeException("Got to 4");
//                });
//        ints.subscribe(System.out::println,
//                error -> System.err.println("Error: " + error)
//        , () -> {System.out.println("Done");});

        SampleSubscriber<Integer> ss = new SampleSubscriber<Integer>();
        Flux<Integer> ints = Flux.range(1, 4);
        ints.subscribe(ss);
        System.out.println(ss.list.size());
    }
}

class SampleSubscriber<T> extends BaseSubscriber<T> {
    List<T> list=new ArrayList<>();
    @Override
    protected void hookOnError(Throwable throwable) {
        System.err.println(throwable.toString());
    }

    public void hookOnSubscribe(Subscription subscription) {
        System.out.println("Subscribed");
        request(1);
    }

    public void hookOnNext(T value) {
        System.out.println("value:"+value);
        list.add(value);
        request(1);
    }

    @Override
    protected void hookOnComplete() {

        System.out.println(list);
    }
}